package com.javaee.xiangyang.service.impl;

import com.javaee.xiangyang.common.ServiceResultEnum;
import com.javaee.xiangyang.entity.Admin;
import com.javaee.xiangyang.entity.User;
import com.javaee.xiangyang.mapper.AdminMapper;
import com.javaee.xiangyang.mapper.UserMapper;
import com.javaee.xiangyang.service.AccountService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {

    @Resource
    UserMapper userMapper;

    @Resource
    AdminMapper adminMapper;

    /**
     * 用户注册实现类
     */
    @Override
    @Transactional
    public ServiceResultEnum AddUserService(User user) {
        if (userMapper.findByUserAccount(user) == null) {
            userMapper.addUser(user);
            return null;
        }
        return ServiceResultEnum.SAME_REGISTER_NAME_EXIST;
    }

    /**
     * 管理员获取所有用户
     */
    @Override
    public List<User> getAllUser() {
        return userMapper.findAllUser();
    }

    /**
     * 管理员根据用户状态获取用户信息
     */
    @Override
    public List<User> getAllUserByState(User user, String string) {
        return userMapper.findAllUserByState(user, string);
    }


    /**
     * 用户登录实现类
     */
    @Override
    public ServiceResultEnum userLoginService(User user) {
        User byUserAccount = userMapper.findByUserAccount(user);
        if (byUserAccount != null) {
            //数据库有此用户
            if (byUserAccount.getUserPassword().equals(user.getUserPassword())) {
                //密码正确
                if (byUserAccount.getUserState().getCode() == 1) {
                    return ServiceResultEnum.LOGIN_SUCCESS;
                }
                //被封禁
                return ServiceResultEnum.LOGIN_USER_LOCKED;
            }
            return ServiceResultEnum.LOGIN_PASSWORD_NULL;
        } else {
            //数据库没有此用户
            return ServiceResultEnum.LOGIN_NAME_NULL;
        }
    }

    /**
     * 管理员增加用户存款
     */
    @Override
    public ServiceResultEnum userUpdateMoney(User user) {
        User byUserAccount = userMapper.findByUserAccount(user);
        if (byUserAccount != null){
            //用户存在
            userMapper.updateMoneyByUserAccount(user,true);
            return ServiceResultEnum.SUCCESS;
        }
        return ServiceResultEnum.LOGIN_NAME_NULL;
    }

    /**
     * 获取用户信息实现类
     */
    @Override
    public User GetUser(User user) {
        return userMapper.findByUserAccount(user);
    }

    /**
     * 用户修改个人信息实现类
     */
    @Override
    @Transactional
    public ServiceResultEnum userUpdateService(User user) {
        if (userMapper.findByUserAccount(user) != null) {
            userMapper.updateByUserAccount(user);
            return ServiceResultEnum.SUCCESS;
        }
        return ServiceResultEnum.LOGIN_NAME_NULL;
    }

    @Override
    public ServiceResultEnum userUpdateHead(User user) {
        if (userMapper.findByUserAccount(user) != null) {
            userMapper.updateByUserHeadAccount(user);
            return ServiceResultEnum.SUCCESS;
        }
        return ServiceResultEnum.LOGIN_NAME_NULL;
    }

    /**
     * 管理员修改用户帐号状态
     */
    @Override
    @Transactional
    public ServiceResultEnum userUpdateState(User user) {
        if (userMapper.findByUserAccount(user) != null) {
            userMapper.updateStateByUserAccount(user);
            return ServiceResultEnum.SUCCESS;
        }
        return ServiceResultEnum.LOGIN_NAME_NULL;
    }

    /**
     * 管理员登录
     */
    @Override
    public ServiceResultEnum adminLogin(Admin admin) {
        if (adminMapper.findByAdminAccount(admin) != null) {
            //该管理员账号存在
            if (admin.getAdminPassword().equals(adminMapper.findByAdminAccount(admin).getAdminPassword())) {
                //密码正确
                return ServiceResultEnum.SUCCESS;
            }
            //密码不正确
            return ServiceResultEnum.ERROR;
        }
        //用户不存在
        return ServiceResultEnum.LOGIN_NAME_NULL;
    }

    /**
     * 管理员修改个人密码
     */
    @Override
    @Transactional
    public ServiceResultEnum adminUpdatePassword(Admin admin) {
        if (adminMapper.findByAdminAccount(admin) != null) {
            //管理员账号存在
            adminMapper.updateByAdminAccount(admin);
            return ServiceResultEnum.SUCCESS;
        }
        //管理员不存在
        return ServiceResultEnum.LOGIN_NAME_NULL;
    }
}
